package com.springcloud.play.novel.controller;

import com.springcloud.play.novel.model.Novel;
import com.springcloud.play.novel.model.NovelChapter;
import com.springcloud.play.novel.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/novel")
public class NovelController {

    @Autowired
    private NovelService novelService;
    
    /**
     * 爬取小说
     */
    @PostMapping("/crawl")
    public ResponseEntity<Map<String, Object>> crawlNovel(@RequestParam String novelUrl) {
        Map<String, Object> result = new HashMap<>();
        try {
            novelService.crawlNovel(novelUrl);
            result.put("success", true);
            result.put("message", "小说爬取成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "小说爬取失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }
    
    /**
     * 获取小说列表
     */
    @GetMapping("/list")
    public ResponseEntity<List<Novel>> getNovelList() {
        List<Novel> novels = novelService.searchNovels("");
        return ResponseEntity.ok(novels);
    }
    
    /**
     * 获取小说详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<Novel> getNovel(@PathVariable Long id) {
        Novel novel = novelService.getNovelById(id);
        if (novel == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(novel);
    }
    
    /**
     * 获取小说章节列表
     */
    @GetMapping("/{novelId}/chapters")
    public ResponseEntity<List<NovelChapter>> getNovelChapters(@PathVariable Long novelId) {
        List<NovelChapter> chapters = novelService.getNovelChapters(novelId);
        return ResponseEntity.ok(chapters);
    }
    
    /**
     * 获取章节内容
     */
    @GetMapping("/chapter/{chapterId}")
    public ResponseEntity<NovelChapter> getChapterContent(@PathVariable Long chapterId) {
        NovelChapter chapter = novelService.getNovelChapter(chapterId);
        if (chapter == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(chapter);
    }
    
    /**
     * 搜索小说
     */
    @GetMapping("/search")
    public ResponseEntity<List<Novel>> searchNovels(@RequestParam String keyword) {
        List<Novel> novels = novelService.searchNovels(keyword);
        return ResponseEntity.ok(novels);
    }
    
    /**
     * 手动触发定时任务
     */
    @PostMapping("/scheduled/crawl")
    public ResponseEntity<Map<String, Object>> triggerScheduledCrawl() {
        Map<String, Object> result = new HashMap<>();
        try {
            novelService.scheduledCrawl();
            result.put("success", true);
            result.put("message", "定时任务手动触发成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "定时任务手动触发失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }
}